//参数表示生成验证码的位数
export function randomString(len) {
    //chars中去除了容易混淆的字母或数字，例如O和0
    var chars = '0123456789';
    var maxPos = chars.length;
    var pwd = '';
    for (var i = 0; i < len; i++) {
        pwd += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}


/**
 * 验证手机号是否符合标准
 * @param {手机号} phone 
 */
export function jugeIsPhone(phone) {
    const telStr = /^[1](([3][0-9])|([4][0,1,4-9])|([5][0-3,5-9])|([6][2,5,6,7])|([7][0-8])|([8][0-9])|([9][0-3,5-9]))[0-9]{8}$/;
    if (!(telStr.test(phone))) {
        return false
    } else {
        return true
    }
}

//获取出生年龄，周岁、月、天、时、分、秒
export function getGrowAge(birthday) {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();

    var myDate = new Date(birthday);
    var myYear = myDate.getFullYear();
    var myMonth = myDate.getMonth() + 1;
    var myDay = myDate.getDate();
    var myHour = myDate.getHours();
    var myMinute = myDate.getMinutes();
    var mySecond = myDate.getSeconds();

    var gapSecond = second - mySecond;
    if (gapSecond < 0) {
        minute -= 1;
        gapSecond = 60 - mySecond + second;
    }
    var gapMinute = minute - myMinute;
    if (gapMinute < 0) {
        hour -= 1;
        gapMinute = 60 - myMinute + minute;
    }
    var gapHour = hour - myHour;
    if (gapHour < 0) {
        day -= 1;
        gapHour = 24 - myHour + hour;
    }
    var gapDay = day - myDay;
    if (gapDay < 0) {
        month -= 1;
        gapDay = getDaysOfMonth(birthday) - myDay + day;
    }
    var gapMonth = month - myMonth;
    if (gapMonth < 0) {
        year -= 1;
        gapMonth = 12 - myMonth + month;
    }
    var gapYear = year - myYear;
    if (gapYear < 0) {
        gapYear = 0;
    }

    // var dateStr = gapYear+"岁零" + 
    //               (gapMonth<10? ("0"+gapMonth): gapMonth)+"月 " + 
    //               (gapDay<10? ("0"+gapDay): gapDay)+"天 " + 
    //               (gapHour<10? ("0"+gapHour): gapHour)+"时 " + 
    //               (gapMinute<10? ("0"+gapMinute): gapMinute)+"分 " + 
    //               (gapSecond<10? ("0"+gapSecond): gapSecond)+"秒";
    var dateStr = gapYear + "岁零" +
        gapMonth + "月 ";
    return dateStr;
}



//获取出生年龄，周岁、月、天、时、分、秒
export function getGrowAgeYear(birthday) {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();

    var myDate = new Date(birthday);
    var myYear = myDate.getFullYear();
    var myMonth = myDate.getMonth() + 1;
    var myDay = myDate.getDate();
    var myHour = myDate.getHours();
    var myMinute = myDate.getMinutes();
    var mySecond = myDate.getSeconds();

    var gapSecond = second - mySecond;
    if (gapSecond < 0) {
        minute -= 1;
        gapSecond = 60 - mySecond + second;
    }
    var gapMinute = minute - myMinute;
    if (gapMinute < 0) {
        hour -= 1;
        gapMinute = 60 - myMinute + minute;
    }
    var gapHour = hour - myHour;
    if (gapHour < 0) {
        day -= 1;
        gapHour = 24 - myHour + hour;
    }
    var gapDay = day - myDay;
    if (gapDay < 0) {
        month -= 1;
        gapDay = getDaysOfMonth(birthday) - myDay + day;
    }
    var gapMonth = month - myMonth;
    if (gapMonth < 0) {
        year -= 1;
        gapMonth = 12 - myMonth + month;
    }
    var gapYear = year - myYear;
    if (gapYear < 0) {
        gapYear = 0;
    }

    // var dateStr = gapYear+"岁零" + 
    //               (gapMonth<10? ("0"+gapMonth): gapMonth)+"月 " + 
    //               (gapDay<10? ("0"+gapDay): gapDay)+"天 " + 
    //               (gapHour<10? ("0"+gapHour): gapHour)+"时 " + 
    //               (gapMinute<10? ("0"+gapMinute): gapMinute)+"分 " + 
    //               (gapSecond<10? ("0"+gapSecond): gapSecond)+"秒";
    var dateStr = gapYear
    return dateStr;
}


//获取当月的天数
function getDaysOfMonth(dateStr) {
    var date = new Date(dateStr);
    var year = date.getFullYear();
    var mouth = date.getMonth() + 1;
    var day = 0;

    if (mouth == 2) {
        day = isLeapYear(year) ? 29 : 28;
    } else if (mouth == 1 || mouth == 3 || mouth == 5 ||
        mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12) {
        day = 31;
    } else {
        day = 30;
    }
    return day;
}

//判断是否为闰年
function isLeapYear(year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}



export function format(shijianchuo) {
    //shijianchuo是整数，否则要parseInt转换
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    // return y + '年' + add0(m) + '月' + add0(d) + '号' + add0(h) + ':' + add0(mm) + ':' + add0(s);
    return y + '年' + add0(m) + '月' + add0(d) + '号';
}


export function format1(shijianchuo) {
    //shijianchuo是整数，否则要parseInt转换
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    // return y + '年' + add0(m) + '月' + add0(d) + '号' + add0(h) + ':' + add0(mm) + ':' + add0(s);
    return y + '-' + add0(m) + '-' + add0(d);
}

function add0(m) { return m < 10 ? '0' + m : m }








// date.js
export function formatDate(date, fmt) {
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
            let str = o[k] + '';
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return fmt;
};

function padLeftZero(str) {
    return ('00' + str).substr(str.length);
};